﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using Power.DAL;
using Power.Model;

namespace Admin.UI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public string username
        {
            get;
            set;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Rootlogin rotwin = new Rootlogin();
            if (rotwin.ShowDialog() != true)
            {
                Application.Current.Shutdown();
            }

            username = rotwin.rootusername;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            Editroot editwin = new Editroot();
            editwin.USername = username;
          
            editwin.ShowDialog();
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            About abwin = new About();
            abwin.Show();
        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            List<User> uslist = new List<User>();

            User[] userall = new UserDal().ListAll();

            foreach (User usone in userall)
            {
                User p = new User();
                p.UserName = usone.UserName;
                p.Id = usone.Id;
                p.Password = usone.Password;
                p.Usertype = usone.Usertype;
                switch (p.Usertype)
                {
                    case 0:
                        p.Usert = "未激活";
                        break;

                    case 3:
                        p.Usert = "文员";
                        break;

                    case 4:
                        p.Usert = "工程师";
                        break;

                    case 5:
                        p.Usert = "管理员";
                        break;

                    default:
                        p.Usert = "未激活";
                        break;
                }
                p.EmployeeID = usone.EmployeeID;
                uslist.Add(p);
            }
            dg1.ItemsSource = uslist;

        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            List<User> uslist = new List<User>();

            User[] userta = new UserDal().ListToAct();

            foreach (User usone in userta)
            {
                User p = new User();
                p.UserName = usone.UserName;
                p.Id = usone.Id;
                p.Password = usone.Password;
                p.Usertype = usone.Usertype;

                switch (p.Usertype)
                {
                    case 0:
                        p.Usert = "未激活";
                        break;

                    case 3:
                        p.Usert = "文员";
                        break;

                    case 4:
                        p.Usert = "工程师";
                        break;

                    case 5:
                        p.Usert = "管理员";
                        break;

                    default:
                        p.Usert = "未激活";
                        break;
                }
                p.EmployeeID = usone.EmployeeID;
                uslist.Add(p);
            }
            dg1.ItemsSource = uslist;
        }

        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            string texttemp = Findbox.Text;

            List<User> uslist = new List<User>();

            User[] userta = new UserDal().ListToFind(texttemp);

            foreach (User usone in userta)
            {
                User p = new User();
                p.UserName = usone.UserName;
                p.Id = usone.Id;
                p.Password = usone.Password;
                p.Usertype = usone.Usertype;
                switch (p.Usertype)
                {
                    case 0:
                        p.Usert = "未激活";
                        break;

                    case 3:
                        p.Usert = "文员";
                        break;

                    case 4:
                        p.Usert = "工程师";
                        break;

                    case 5:
                        p.Usert = "管理员";
                        break;

                    default:
                        p.Usert = "未激活";
                        break;
                }

                p.EmployeeID = usone.EmployeeID;
                uslist.Add(p);
            }
            dg1.ItemsSource = uslist;
        }

        private void btnAct_Click(object sender, RoutedEventArgs e)
        {

            User temp = (User)dg1.SelectedItem;

            if (temp == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            new UserDal().RootAct(temp.UserName);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            User temp = (User)dg1.SelectedItem;
            EditUser editwin = new EditUser();
            editwin.USername = temp.UserName;
            editwin.Employee = temp.EmployeeID;
            editwin.ShowDialog();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            NewUser nswin = new NewUser();
            nswin.ShowDialog();
        }

        private void btnActAll_Click(object sender, RoutedEventArgs e)
        {
            new UserDal().RootActAll();
        }

        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }

            User temp = (User)dg1.SelectedItem;
            new UserDal().RootDel(temp.UserName);
        }

        private void btnActE_Click(object sender, RoutedEventArgs e)
        {
            User temp = (User)dg1.SelectedItem;
            if (temp == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            new UserDal().RootActE(temp.UserName);
        }

    }
}